package com.springbootblog.controller.fontend;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.springbootblog.service.fontend.ElasticsearchService;
import com.springbootblog.service.fontend.IndexService;
import com.springbootblog.utils.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;

/**
 * 首页部分接口
 * @className AboutController
 * @author camellia
 * @email 805795955@qq.com
 * @date 2024/01/01
 * @version 1.0
 */
@RequestMapping("java")
@RestController
public class IndexController extends BaseController
{
    /**
     * 定义一个静态常量日志对象
     */
    private static final Logger logger = LoggerFactory.getLogger(IndexController.class);

    // 自动装载（service接口注入）
    @Autowired
    private IndexService indexService;
    @Resource
    private ElasticsearchService elasticsearchService;

    /**
     * 注解 @PostConstruct 的含义是：本类构造方法执行结束后执行
     */
    @PostConstruct
    public void initFlowRule()
    {
        // 设置Sentinel规则
        Function.setSentinelRule("AddUser", 1);
    }

    /**
     * 获取博客首页数据
     * @return
     */
    @GetMapping("index/test")
    public Map<String, Object> test()
    {
        try
        {
            // 设置一个资源名称为 Hello
            Entry ignored = SphU.entry("AddUser");
            Map<String, Object> result = new HashMap<>();
            result.put("code", 1);
            result.put("msg", "通过！");
            return result;
        }
        catch (BlockException ex)
        {
            Map<String, Object> result = new HashMap<>();
            result.put("code", -1);
            result.put("msg", "别急，等一会在请求！");
            return result;
        }
    }

    /**
     * 获取博客首页数据
     * @param search 搜索值
     * @param page   页码
     * @return
     */
    @GetMapping("index/getData")
    public Map<String, Object> getData(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "") String search) throws BlockException
    {
        // 设置一个资源名称为 Hello
       //Entry ignored = SphU.entry("AddUser");
        Map<String, Object> result = indexService.getData(page, search);
        return result;
    }

    /**
     *
     * @param page
     * @param search
     * @return
     */
    @GetMapping("index/getArticleListByCategory")
    public Map<String, Object> getArticleListByCategory(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "") String search,@RequestParam(defaultValue = "") String cate_id,@RequestParam(defaultValue = "") String cate_id_son,@RequestParam(defaultValue = "") String label_id) throws Exception {
        if((search == "" || search == null) && (cate_id == "" || cate_id == null) && (cate_id_son == "" || cate_id_son == null) && (label_id == "" || label_id == null))
        {
            Map<String, Object> result = new HashMap<>(2);
            result.put("code",-1);
            result.put("msg","参数错误！");
            return result;
        }
        Map<String, Object> result;
        if(!search.isEmpty() && !search.equals(null))
        {
            try
            {//*/
                 System.out.println("elasticsearch 查询");
                // elasticsearch 查询
                String[] field = {"content","arttitle","artdesc"};
                result = elasticsearchService.mapSearch("test", "90", search, page, field);
            }
            catch(Exception ex)
            {
                // 数据库查询
                result = indexService.getArticleListByCategory(page, search, cate_id, cate_id_son, label_id);
            }//*/
            // elasticsearch 搜索值为空，调用数据库查询
            List<Object> list = (List<Object>) result.get("articleShow");
            if(list.isEmpty())
            {
                // 数据库查询
                result = indexService.getArticleListByCategory(page, search, cate_id, cate_id_son, label_id);
            }
        }
        else
        {
            // 数据库查询
            result = indexService.getArticleListByCategory(page, search, cate_id, cate_id_son, label_id);
        }
        return result;
    }




}
